﻿using iChip.Hostpots.Models;
using iChip.Hotspots.Commons;
using iChip.Hotspots.DB;
using iChip.Hotspots.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace iChip.Hotspots.BO
{
    public class BOs
    {
        //private readonly MainDB _db = new MainDB();

        #region FrontEnd
        public IndexObject Get_Field_by_Hotspot_Mac(string Hotspot_mac, int user_type)
        {
            MainDB _db = new MainDB();
            var obj = new IndexObject();

            DataSet ds = new DataSet();
            ds = _db.StoredProcedures.Get_Field_by_Hotspot_Mac(Hotspot_mac, user_type);
            if(ds!=null)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count>0)
                {
                    obj.Splash = new Splash
                    {
                        Splash_Content = ds.Tables[0].Rows[0]["Splash_Content"].ToString(),
                        Splash_Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Splash_Id"]),
                        Splash_Name = ds.Tables[0].Rows[0]["Splash_Name"].ToString(),
                        User_Type = Convert.ToInt32(ds.Tables[0].Rows[0]["User_Type"]),
                        Splash_UrlRedirect = ds.Tables[0].Rows[0]["Splash_UrlRedirect"].ToString()
                    };
                }

                if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                {
                    obj.Fields = new List<Field>();
                    foreach (DataRow dr in ds.Tables[1].Rows)
                    {
                        obj.Fields.Add(new Field
                        {
                            Field_Id = Convert.ToInt32(dr["Field_Id"]),
                            Field_Name = dr["Field_Name"].ToString(),
                            FieldType_Id = Convert.ToInt32(dr["FieldType_Id"]),
                            Field_IsRequire = Convert.ToBoolean(dr["Field_IsRequire"])
                        });
                    }
                }
            }

            _db.Close();
            _db.Dispose();

            return obj;
        }

        public IndexObject Select_Splash(string Hotspot_mac, string user_mac)
        {
            MainDB _db = new MainDB();
            var obj = new IndexObject();

            DataSet ds = new DataSet();
            ds = _db.StoredProcedures.Select_FE_Hotspot_Mac(Hotspot_mac, user_mac);
            if (ds != null)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    obj.Splash = new Splash
                    {
                        Splash_Content = ds.Tables[0].Rows[0]["Splash_Content"].ToString(),
                        Splash_Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Splash_Id"]),
                        Splash_Name = ds.Tables[0].Rows[0]["Splash_Name"].ToString(),
                        User_Type = Convert.ToInt32(ds.Tables[0].Rows[0]["User_Type"]),
                        Splash_UrlRedirect = ds.Tables[0].Rows[0]["Splash_UrlRedirect"].ToString()
                    };
                }

                if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                {
                    obj.Fields = new List<Field>();
                    foreach (DataRow dr in ds.Tables[1].Rows)
                    {
                        obj.Fields.Add(new Field
                        {
                            Field_Id = Convert.ToInt32(dr["Field_Id"]),
                            Field_Name = dr["Field_Name"].ToString(),
                            FieldType_Id = Convert.ToInt32(dr["FieldType_Id"]),
                            Field_IsRequire = Convert.ToBoolean(dr["Field_IsRequire"])
                        });                                               
                    }                    

                    obj.Method_Type = ds.Tables[1].Rows[0]["Method_Type"].ToString();
                }

                if(ds.Tables[2]!=null && ds.Tables[2].Rows.Count>0)
                {
                    obj.User_Type = ds.Tables[2].Rows[0][0].ToString();
                }

                if (ds.Tables[3] != null && ds.Tables[3].Rows.Count > 0)
                {
                    var objFieldValues = new List<FieldValue>();
                    foreach (DataRow drValue in ds.Tables[3].Rows)
                    {
                        obj.FieldValues.Add(new FieldValue
                        {
                            Field_Id = Convert.ToInt32(drValue["Field_Id"]),
                            Field_Name = drValue["Field_Name"].ToString(),
                            FieldValue_Value = drValue["FieldValue_Value"].ToString(),
                            FieldValue_Id = Convert.ToInt32(drValue["FieldValue_Id"]),
                        });
                    }
                }
            }

            _db.Close();
            _db.Dispose();

            return obj;
        }

        public void Logs(Logs logs)
        {
            MainDB _db = new MainDB();

            _db.StoredProcedures.Insert_Logs(logs.Hotspot_Mac, logs.User_Mac, logs.Log_OS, logs.Log_Device, logs.Method_Type, logs.Log_Brower);

            _db.Close();
            _db.Dispose();
        }

        public void UpdateUser(User user)
        {
            MainDB _db = new MainDB();

            _db.StoredProcedures.Update_Users(user.User_Mac, user.User_Name, user.User_Email, user.User_Phone, user.User_Address, user.User_Link, user.User_Status, user.User_Birthday, user.User_Type);

            _db.Close();
            _db.Dispose();
        }

        #endregion

        #region Admin
     
        #region Logs

        /// <summary>
        /// Hiển thị danh sách Logs
        /// </summary>
        /// <param name="OffsetRows"></param>
        /// <param name="FetchRows"></param>
        /// <returns></returns>
        public List<Log> Select_Logs_Paging(int OffsetRows, int FetchRows)
        {

            var result = new List<Log>();
            MainDB _db = new MainDB();

            DataTable dt = _db.StoredProcedures.Select_Logs_Paging(OffsetRows, FetchRows);

            if (dt != null && dt.Rows.Count > 0)
            {
                int counter = OffsetRows;
                foreach (DataRow dr in dt.Rows)
                {
                    result.Add(new Log
                    {
                        Counter = counter,
                        Log_Id = Convert.ToInt32(dr["Log_Id"].ToString()),
                        Log_Brower = dr["Log_Brower"].ToString(),
                        Log_Device = dr["Log_Device"].ToString(),
                        Method_Type = dr["Method_Type"].ToString(),
                        Log_Time = Convert.ToDateTime(dr["Log_Time"].ToString()),
                        Hotspot_Mac = dr["Hotspot_Mac"].ToString(),
                        User_Mac = dr["User_Mac"].ToString(),
                        Hotspot_Name = dr["Hotspot_Name"].ToString(),

                    });

                    counter++;
                }
            }

            _db.Close();
            _db.Dispose();
            return result;
        }

        /// <summary>
        /// Thống kế Log trong tháng theo ngày
        /// </summary>
        /// <param name="Month"></param>
        /// <param name="Year"></param>
        /// <returns></returns>
        public List<Chart_LogByDay> Select_Logs_GroupBy_Day(int Month, int Year)
        {
            try
            {
                MainDB _db = new MainDB();
                var result = new List<Chart_LogByDay>();

                DataTable dt = new DataTable();
                dt = _db.StoredProcedures.Select_Logs_GroupBy_Day(Month, Year);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        result.Add(new Chart_LogByDay
                        {
                            Date = int.Parse(dr["Date"].ToString()),
                            Total = int.Parse(dr["Total"].ToString())                         

                        });
                    }
                }

                _db.Close();
                _db.Dispose();
                return result;
            }
            catch (Exception ex)
            {

                return null;
            }
        }


        #endregion

        #region Hotspot


        public HotspotsObject Select_Hotspots_by_Id(int Hotspot_Id)
        {
            MainDB _db = new MainDB();
            var obj = new HotspotsObject();

            DataTable dt = _db.StoredProcedures.Select_Hotspots_by_Id(Hotspot_Id);
            if (dt != null && dt.Rows.Count > 0)
            {
                obj = new HotspotsObject
                {
                    Hotspot_Address = dt.Rows[0]["Hotspot_Address"].ToString(),
                    Hotspot_City = dt.Rows[0]["Hotspot_City"].ToString(),
                    Hotspot_Id = Convert.ToInt32(dt.Rows[0]["Hotspot_Id"]),
                    Hotspot_Ip = dt.Rows[0]["Hotspot_Ip"].ToString(),
                    Hotspot_Location = dt.Rows[0]["Hotspot_Location"].ToString(),
                    Hotspot_Mac = dt.Rows[0]["Hotspot_Mac"].ToString(),
                    Hotspot_Name = dt.Rows[0]["Hotspot_Name"].ToString(),
                    Hotspot_Nasid = dt.Rows[0]["Hotspot_Nasid"].ToString(),
                    Hotspot_Region = dt.Rows[0]["Hotspot_Region"].ToString(),
                    Hotspot_Status = Convert.ToBoolean(dt.Rows[0]["Hotspot_Status"]),
                    Hotspot_Type = dt.Rows[0]["Hotspot_Type"].ToString(),
                };
            }

            _db.Close();
            _db.Dispose();

            return obj;
        }

        public void Delete_Hotspots(int Hotspot_Id)
        {
            MainDB _db = new MainDB();

            _db.StoredProcedures.Delete_Hotspots(Hotspot_Id);

            _db.Close();
            _db.Dispose();
        }

        public List<HotspotsObject> Select_Hotspots_Paging(int OffsetRows, int FetchRows)
        {

            var result = new List<HotspotsObject>();
            MainDB _db = new MainDB();

            DataTable dt = _db.StoredProcedures.Select_Hotspots_Paging(OffsetRows, FetchRows);

            if (dt != null && dt.Rows.Count > 0)
            {
                int counter = OffsetRows;
                foreach (DataRow dr in dt.Rows)
                {
                    result.Add(new HotspotsObject
                    {
                        Counter = counter,
                        Hotspot_Address = dr["Hotspot_Address"].ToString(),
                        Hotspot_City = dr["Hotspot_City"].ToString(),
                        Hotspot_Id = Convert.ToInt32(dr["Hotspot_Id"]),
                        Hotspot_Ip = dr["Hotspot_Ip"].ToString(),
                        Hotspot_Location = dr["Hotspot_Location"].ToString(),
                        Hotspot_Mac = dr["Hotspot_Mac"].ToString(),
                        Hotspot_Name = dr["Hotspot_Name"].ToString(),
                        Hotspot_Nasid = dr["Hotspot_Nasid"].ToString(),
                        Hotspot_Region = dr["Hotspot_Region"].ToString(),
                        Hotspot_Status = Convert.ToBoolean(dr["Hotspot_Status"]),
                        Hotspot_Type = dr["Hotspot_Type"].ToString(),
                    });

                    counter++;
                }
            }

            _db.Close();
            _db.Dispose();
            return result;
        }

        public bool Insert_Hotspots(HotspotsModel model, out string mess)
        {
            try
            {
                MainDB _db = new MainDB();
                bool flag = false;
                DataTable dt = _db.StoredProcedures.Insert_Hotspots(model.Hotspot_Mac, model.Hotspot_Ip, model.Hotspot_Nasid, model.Hotspot_Status, model.Hotspot_City, model.Hotspot_Region, model.Hotspot_Address, model.Hotspot_Location, model.Hotspot_Name, model.Hotspot_Type, model.Account_Id);

                if (dt != null && dt.Rows.Count > 0)
                {
                    if (Convert.ToInt32(dt.Rows[0][0]) > 0)
                    {
                        // Tao mới 1 splash mặc định
                        var splash = new SplashModel
                        {
                            Hotspot_Id = Convert.ToInt32(dt.Rows[0][0]),
                            Hotspot_Mac = model.Hotspot_Mac,
                            Splash_Name = "Splash " + model.Hotspot_Name,
                            Splash_StartTime = DateTime.Now,
                            Splash_EndTime = DateTime.Now.AddYears(10),
                            Splash_IsDefault = true,
                            User_Type = 0,

                        };
                        Insert_Splashs(splash);
                        flag = true;
                        mess = string.Empty;
                    }
                    else
                    {
                        mess = "Đã tồn tại Hotspot Mac này rồi!";
                        flag = false;
                    }
                }
                else
                {
                    mess = "Không thêm mới được Hotspot!";
                }

                _db.Close();
                _db.Dispose();
                return flag;
            }
            catch (Exception ex)
            {
                mess = ex.Message;
                return false;
            }
        }

        public bool Update_Hotspots(HotspotsModel model, out string mess)
        {
            try
            {
                MainDB _db = new MainDB();

                _db.StoredProcedures.Update_Hotspots(model.Hotspot_Id, model.Hotspot_Mac, model.Hotspot_Ip, model.Hotspot_Nasid, model.Hotspot_Status, model.Hotspot_City, model.Hotspot_Region, model.Hotspot_Address, model.Hotspot_Location, model.Hotspot_Name, model.Hotspot_Type);

                _db.Close();
                _db.Dispose();
                mess = string.Empty;
                return true;
            }
            catch (Exception ex)
            {
                mess = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// Update LiveTime Hotspot
        /// </summary>
        /// <param name="model"></param>
        /// <param name="mess"></param>
        /// <returns></returns>
        public bool Update_Hotspots_LiveTime(string mac, string ip, string nasId, out string mess)
        {
            try
            {
                MainDB _db = new MainDB();

                _db.StoredProcedures.Update_Hotspots_LiveTime(mac, ip, nasId);

                _db.Close();
                _db.Dispose();
                mess = string.Empty;
                return true;
            }
            catch (Exception ex)
            {
                mess = ex.Message;
                return false;
            }
        }

        public List<HotspotsObject> Select_Hotspots_Down()
        {
            try
            {
                MainDB _db = new MainDB();
                var result = new List<HotspotsObject>();

                DataTable dt = new DataTable();
                dt = _db.StoredProcedures.Select_Hotspots_Down();
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        result.Add(new HotspotsObject
                        {
                            Hotspot_Address = dr["Hotspot_Address"].ToString(),
                            Hotspot_City = dr["Hotspot_City"].ToString(),
                            Hotspot_Id = Convert.ToInt32(dr["Hotspot_Id"]),
                            Hotspot_Ip = dr["Hotspot_Ip"].ToString(),
                            Hotspot_Location = dr["Hotspot_Location"].ToString(),
                            Hotspot_Mac = dr["Hotspot_Mac"].ToString(),
                            Hotspot_Name = dr["Hotspot_Name"].ToString(),
                            Hotspot_Nasid = dr["Hotspot_Nasid"].ToString(),
                            Hotspot_Region = dr["Hotspot_Region"].ToString(),
                            Hotspot_Status = Convert.ToBoolean(dr["Hotspot_Status"]),
                            Hotspot_Type = dr["Hotspot_Type"].ToString(),

                        });
                    }
                }

                _db.Close();
                _db.Dispose();
                return result;
            }
            catch (Exception ex)
            {

                return null;
            }
        }

        public List<HotspotsObject> Select_Hotspots()
        {
            try
            {
                MainDB _db = new MainDB();
                var result = new List<HotspotsObject>();

                DataTable dt = new DataTable();
                dt = _db.StoredProcedures.Select_Hotspots();
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        result.Add(new HotspotsObject
                        {
                            Hotspot_Address = dr["Hotspot_Address"].ToString(),
                            Hotspot_City = dr["Hotspot_City"].ToString(),
                            Hotspot_Id = Convert.ToInt32(dr["Hotspot_Id"]),
                            Hotspot_Ip = dr["Hotspot_Ip"].ToString(),
                            Hotspot_Location = dr["Hotspot_Location"].ToString(),
                            Hotspot_Mac = dr["Hotspot_Mac"].ToString(),
                            Hotspot_Name = dr["Hotspot_Name"].ToString(),
                            Hotspot_Nasid = dr["Hotspot_Nasid"].ToString(),
                            Hotspot_Region = dr["Hotspot_Region"].ToString(),
                            Hotspot_Status = Convert.ToBoolean(dr["Hotspot_Status"]),
                            Hotspot_Type = dr["Hotspot_Type"].ToString(),

                        });
                    }
                }

                _db.Close();
                _db.Dispose();
                return result;
            }
            catch (Exception ex)
            {

                return null;
            }
        }


        #endregion

        #region Splash


        public void Delete_Splashs(int Splash_Id)
        {
            MainDB _db = new MainDB();

            _db.StoredProcedures.Delete_Splashs(Splash_Id);

            _db.Close();
            _db.Dispose();
        }


        public List<Splash> Select_Splash_Paging(int OffsetRows, int FetchRows, int Hotspot_Id)
        {
            MainDB _db = new MainDB();
            var result = new List<Splash>();

            DataTable dt = new DataTable();
            dt = _db.StoredProcedures.Select_Splash_Paging(OffsetRows, FetchRows, Hotspot_Id);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    result.Add(new Splash
                    {
                        Hotspot_Mac = dr["Hotspot_Mac"].ToString(),
                        Splash_Content = dr["Splash_Content"].ToString(),
                        Splash_EndTime = Convert.ToDateTime(dr["Splash_EndTime"]),
                        Splash_Id = Convert.ToInt32(dr["Splash_Id"]),
                        Splash_Name = dr["Splash_Name"].ToString(),
                        Splash_StartTime = Convert.ToDateTime(dr["Splash_StartTime"]),
                        Splash_UrlRedirect = dr["Splash_UrlRedirect"].ToString(),
                        User_Type = Convert.ToInt32(dr["User_Type"]),
                        Splash_IsDefault = Convert.ToBoolean(dr["Splash_IsDefault"])
                    });
                }
            }

            _db.Close();
            _db.Dispose();

            return result;

        }

        public bool Insert_Splashs(SplashModel model)
        {
            MainDB _db = new MainDB();

            DataTable dt = _db.StoredProcedures.Insert_Splashs(model.Hotspot_Mac, model.Hotspot_Id, model.Splash_Name, model.Splash_StartTime, model.Splash_EndTime, model.User_Type, model.Splash_Content, model.Splash_UrlRedirect, model.Splash_IsDefault);

            // Tao luon method
            if (dt != null && dt.Rows.Count > 0)
            {
                int splash_id = Convert.ToInt32(dt.Rows[0][0]);
                for (int i = 0; i < Commons.Commons.MethodType.Length; i++)
                {
                    _db.StoredProcedures.Insert_Methods(splash_id, Commons.Commons.MethodType[i], i, true);
                }
            }

            _db.Close();
            _db.Dispose();
            return true;


        }

        public bool Update_Splashs(SplashModel model)
        {
            try
            {
                MainDB _db = new MainDB();

                _db.StoredProcedures.Update_Splashs(model.Splash_Id, model.Hotspot_Mac, model.Hotspot_Id, model.Splash_Name, model.Splash_StartTime, model.Splash_EndTime, model.User_Type, model.Splash_Content, model.Splash_UrlRedirect);

                _db.Close();
                _db.Dispose();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
        public SplashModel Select_Splash_by_Id(int Splash_Id)
        {
            var obj = new SplashModel();

            MainDB _db = new MainDB();
            DataTable dt = _db.StoredProcedures.Select_Splash_by_Id(Splash_Id);

            if (dt != null && dt.Rows.Count > 0)
            {
                obj = new SplashModel
                {
                    Hotspot_Mac = dt.Rows[0]["Hotspot_Mac"].ToString(),
                    Splash_Content = dt.Rows[0]["Splash_Content"].ToString(),
                    Splash_EndTime = Convert.ToDateTime(dt.Rows[0]["Splash_EndTime"]),
                    Splash_Id = Convert.ToInt32(dt.Rows[0]["Splash_Id"]),
                    Splash_Name = dt.Rows[0]["Splash_Name"].ToString(),
                    Splash_StartTime = Convert.ToDateTime(dt.Rows[0]["Splash_StartTime"]),
                    Splash_UrlRedirect = dt.Rows[0]["Splash_UrlRedirect"].ToString(),
                    User_Type = Convert.ToInt32(dt.Rows[0]["User_Type"]),
                    Hotspot_Id = Convert.ToInt32(dt.Rows[0]["Hotspot_Id"]),
                    Splash_IsDefault = Convert.ToBoolean(dt.Rows[0]["Splash_IsDefault"])
                };
            }

            return obj;

        }


        #endregion

        #region Field & Value

        /// <summary>
        /// Danh sach FieldType lay tu Enum
        /// </summary>
        /// <returns></returns>
        private IEnumerable<SelectListItem> Get_FieldType_List()
        {
            IEnumerable<iChip.Hotspots.Commons.Enums.FieldType> fieldType = Enum.GetValues(typeof(iChip.Hotspots.Commons.Enums.FieldType)).Cast<iChip.Hotspots.Commons.Enums.FieldType>();

            var obj = fieldType.Select(m => new SelectListItem
            {
                Text = m.ToString(),
                Value = ((int)m).ToString()
            }).ToList();

            return obj;
        }


        public void Insert_Values(string User_Mac, List<RequestPostField> objRequest)
        {
            MainDB _db = new MainDB();
            if (objRequest != null)
            {
                foreach (var item in objRequest)
                {
                    _db.StoredProcedures.Insert_Values(User_Mac, item.Id, item.Value, item.ValueParent_Id);
                }
            }

            _db.Close();
            _db.Dispose();
        }

        public void Insert_Fields(List<RequestPostField> objRequest, List<FieldValue> objRequestFieldValue)
        {


            if (objRequest != null)
            {
                MainDB _db = new MainDB();
                var objFieldValueTemp = new FieldValue();
                foreach (var item in objRequest)
                {
                    if (!string.IsNullOrEmpty(item.Name) && !string.IsNullOrEmpty(item.Value))
                    {
                        if (!string.IsNullOrEmpty(item.Name))
                        {
                            _db.StoredProcedures.Insert_Fields(item.Id, item.Method_Id, item.Name, Convert.ToInt32(item.Value), item.IsRequire);

                            // luu FieldValue                        
                            if (objRequestFieldValue != null && objRequestFieldValue.Count > 0)
                            {
                                foreach (var item2 in objRequestFieldValue)
                                {
                                    if (item.Id == item2.Field_Id)
                                    {
                                        if (!string.IsNullOrEmpty(item2.FieldValue_Value))
                                        {
                                            _db.StoredProcedures.Insert_FieldValues(item2.FieldValue_Id, item.Id, item.Name, item2.FieldValue_Value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                _db.Close();
                _db.Dispose();
            }


        }


        public void Delete_Field_by_Method_Ids(int Method_Id)
        {
            using (MainDB _db = new MainDB())
            {
                _db.StoredProcedures.Delete_Field_by_Method_Ids(Method_Id);

                _db.Close();
                _db.Dispose();
            }
        }

        public bool Delete_Field_by_Id(int Field_Id)
        {
            try
            {
                MainDB _db = new MainDB();
                _db.StoredProcedures.Delete_Field_by_Id(Field_Id);
                _db.Close();
                _db.Dispose();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public List<FieldValue> Select_FieldValue_by_FieldId(int Field_Id)
        {
            MainDB _db = new MainDB();
            var obj = new List<FieldValue>();
            DataTable dt = new DataTable();
            dt = _db.StoredProcedures.Select_FieldValue_by_FieldId(Field_Id);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    obj.Add(new FieldValue
                    {
                        Field_Id = Field_Id,
                        Field_Name = dr["Field_Name"].ToString(),
                        FieldValue_Id = string.IsNullOrEmpty(dr["FieldValue_Id"].ToString()) ? 0 : Convert.ToInt32(dr["FieldValue_Id"]),
                        FieldValue_Value = dr["FieldValue_Value"].ToString()
                    });
                }
            }
            return obj;
        }


        #endregion

        #region Method

        public List<Method> Select_Method_by_SplashId(int Splash_Id)
        {
            var obj = new List<Method>();
            MainDB _db = new MainDB();
            DataTable dt = _db.StoredProcedures.Select_Method_by_SplashId(Splash_Id);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    obj.Add(new Method
                    {
                        Method_Id = Convert.ToInt32(dr["Method_id"]),
                        Method_Order = Convert.ToInt32(dr["Method_Order"]),
                        Method_Status = Convert.ToBoolean(dr["Method_Status"]),
                        Method_Type = dr["Method_Type"].ToString(),
                        Splash_Id = Splash_Id
                    });
                }
            }
            _db.Close();
            _db.Dispose();
            return obj;
        }
        /// <summary>
        /// Lay thong danh sach cac method va cac field theo tung method theo moi Splash
        /// </summary>
        /// <param name="Splash_Id"></param>
        /// <returns></returns>
        public FieldModel Select_Method_Field_by_SplashId(int Splash_Id)
        {
            var obj = new FieldModel();
            MainDB _db = new MainDB();

            DataSet ds = _db.StoredProcedures.Select_Method_Field_by_SplashId(Splash_Id);
            if (ds != null)
            {
                // danh sach Method
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        obj.Methods.Add(new Method
                        {
                            Method_Id = Convert.ToInt32(dr["Method_id"]),
                            Method_Order = Convert.ToInt32(dr["Method_Order"]),
                            Method_Status = Convert.ToBoolean(dr["Method_Status"]),
                            Method_Type = dr["Method_Type"].ToString(),
                            Method_Description = dr["Method_Description"].ToString(),
                            Splash_Id = Splash_Id
                        });
                    }
                }

                if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[1].Rows)
                    {
                        obj.Fields.Add(new Field
                        {
                            Field_Id = Convert.ToInt32(dr["Field_Id"]),
                            Field_Name = dr["Field_Name"].ToString(),
                            FieldType_Id = Convert.ToInt32(dr["FieldType_Id"]),
                            Field_IsRequire = Convert.ToBoolean(dr["Field_IsRequire"]),
                        });
                    }
                }
            }

            _db.Close();
            _db.Dispose();
            return obj;
        }
        public void Update_Methods(Method model)
        {
            MainDB _db = new MainDB();
            _db.StoredProcedures.Update_Methods(model.Method_Id, model.Splash_Id, model.Method_Type, model.Method_Order, model.Method_Status);
            _db.Close();
            _db.Dispose();
        }

        #endregion

        #region Brander

        public bool Insert_Branders(BranderObject branderObject)
        {
            bool flag = false;
            MainDB _db = new MainDB();
            try
            {
                DataTable dt = new DataTable();
                dt = _db.StoredProcedures.Insert_Branders(branderObject.Brander_Id, branderObject.Brander_Name, branderObject.Brander_User, branderObject.Brander_Password,
                    branderObject.Brander_Email, branderObject.Brander_Phone, branderObject.Brander_Avatar, branderObject.Brander_Fanpage, branderObject.Brander_Facebook_AppId,
                    branderObject.Brander_Facebook_AppSecret, branderObject.Brander_FaceBook_ShareName, branderObject.Brander_Facebook_ShareLink, branderObject.Brander_FaceBook_SharePicture,
                    branderObject.Brander_FaceBook_ShareCaption, branderObject.Brander_FaceBook_ShareDescription, branderObject.Brander_Video_Link, branderObject.Brander_App_Link, branderObject.Brander_Access_Code,
                    branderObject.Brander_StartTime, branderObject.Brander_EndTime,branderObject.Brander_Status, branderObject.Brander_SplashDefault, branderObject.Brander_UrlRedirect);

                flag = true;
            }
            catch
            {
                flag = false;
            }

            _db.Dispose();
            _db.Close();
            return flag;
        }

        public List<BranderObject> Select_Brander_Paging(int OffsetRows, int FetchRows)
        {
            var objBrander = new List<BranderObject>();
            MainDB _db = new MainDB();
            try
            {
                DataTable dt = new DataTable();
                dt = _db.StoredProcedures.Select_Brander_Paging(OffsetRows, FetchRows);
                if(dt!=null && dt.Rows.Count>0)
                {                    
                    foreach(DataRow dr in dt.Rows)
                    {
                        objBrander.Add(new BranderObject
                        {
                            Brander_Access_Code = dr["Brander_Access_Code"].ToString(),
                            Brander_App_Link = dr["Brander_App_Link"].ToString(),
                            Brander_Avatar = dr["Brander_Avatar"].ToString(),
                            Brander_Email = dr["Brander_Email"].ToString(),
                            Brander_EndTime = dr["Brander_EndTime"] == null ? DateTime.Now : Convert.ToDateTime(dr["Brander_EndTime"]),
                            Brander_Facebook_AppId = dr["Brander_Facebook_AppId"].ToString(),
                            Brander_Facebook_AppSecret = dr["Brander_Facebook_AppSecret"].ToString(),
                            Brander_FaceBook_ShareCaption = dr["Brander_FaceBook_ShareCaption"].ToString(),
                            Brander_FaceBook_ShareDescription = dr["Brander_FaceBook_ShareDescription"].ToString(),
                            Brander_Facebook_ShareLink = dr["Brander_Facebook_ShareLink"].ToString(),
                            Brander_FaceBook_ShareName = dr["Brander_FaceBook_ShareName"].ToString(),
                            Brander_FaceBook_SharePicture = dr["Brander_FaceBook_SharePicture"].ToString(),
                            Brander_Fanpage = dr["Brander_Fanpage"].ToString(),
                            Brander_Id = Convert.ToInt32(dr["Brander_Id"]),
                            Brander_Name = dr["Brander_Name"].ToString(),
                            Brander_Password = dr["Brander_Password"].ToString(),
                            Brander_Phone = dr["Brander_Phone"].ToString(),
                            Brander_SplashDefault = dr["Brander_SplashDefault"].ToString(),
                            Brander_StartTime = dr["Brander_StartTime"] == null ? DateTime.Now : Convert.ToDateTime(dr["Brander_StartTime"]),
                            Brander_Status = dr["Brander_Status"]==null ? false : Convert.ToBoolean(dr["Brander_Status"]),
                            Brander_UrlRedirect = dr["Brander_UrlRedirect"].ToString(),
                            Brander_User = dr["Brander_User"].ToString(),
                            Brander_Video_Link = dr["Brander_Video_Link"].ToString(),
                        });
                    }
                }
            }
            catch
            {

            }

            _db.Dispose();
            _db.Close();
            return objBrander;
        }

        #endregion

        #region Account
        public void Insert_Accounts(AccountModel model)
        {
            MainDB _db = new MainDB();
            try
            {
                _db.StoredProcedures.Insert_Accounts(model.Account_Name, model.Password, model.Account_FulNname, model.Account_Status, model.Account_Email, model.Account_Phone, model.Account_Avatar, model.Account_SuperAdmin, model.Brander_Id, model.Account_Role);
            }
            catch
            {

            }

            _db.Close();
            _db.Dispose();
        }
        #endregion
        #endregion



    }
}